package JFrame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * 主界面
 */
public class HomePage extends JPanel {
	private static final long serialVersionUID = 3227223377205085910L;
	

	JButton bt1,bt2,bt3,bt4,bt5,bt6,update;
	JLabel bg,line,e1,e2,e3,e4,e5,all,profit,allN,profitN,title;
	
	CircleProgressBar circleProgressBar1;//圆环进度条
	public HomePage() {
		this.setLayout(null); // 绝对布局
		ImageIcon img1 = new ImageIcon(HomePage.class.getResource("open.png"));
		ImageIcon img2 = new ImageIcon(HomePage.class.getResource("data.png"));
		ImageIcon img3 = new ImageIcon(HomePage.class.getResource("purchase1.png"));
		ImageIcon img4 = new ImageIcon(HomePage.class.getResource("sales.png"));
		ImageIcon img5 = new ImageIcon(HomePage.class.getResource("user.png"));
		ImageIcon img6 = new ImageIcon(HomePage.class.getResource("close.png"));
		ImageIcon background = new ImageIcon(getClass().getResource("userbg.jpg"));
		ImageIcon l = new ImageIcon(getClass().getResource("line.gif"));
		ImageIcon ud = new ImageIcon(getClass().getResource("update.png"));
		
		bt1 = new JButton(img1);
		bt2 = new JButton(img2);
		bt3 = new JButton(img3);
		bt4 = new JButton(img4);
		bt5 = new JButton(img5);
		bt6 = new JButton(img6);
		update = new JButton(ud);
		bg = new JLabel(background);
		line = new JLabel(l);
		circleProgressBar1 = new CircleProgressBar();
		circleProgressBar1.setSize(new Dimension(300, 300));
		circleProgressBar1.setOpaque(false); //透明
		e1 = new JLabel("菜单");
		e2 = new JLabel("库存管理");
		e3 = new JLabel("进货单");
		e4 = new JLabel("销售单");
		e5 = new JLabel("用户管理");
		all = new JLabel("销售总额");
		allN = new JLabel("￥658480");//因为数据库设计上的限制，只能展示自定义数字了
		profit = new JLabel("总利润");
		profitN = new JLabel("￥98660");
		title = new JLabel("阶段目标完成度");
		
		bt1.setBounds(69, 5, 64, 64);
		bt2.setBounds(143, 5, 64, 64);
		bt3.setBounds(207, 5, 64, 64);
		bt4.setBounds(271, 5, 64, 64);
		bt5.setBounds(335, 5, 64, 64);
		bt6.setBounds(399, 21, 32, 32);
		circleProgressBar1.setBounds(400, 130, 300, 300);
		update.setBounds(530, 430, 32, 32);
		line.setBounds(0, 25, 400, 57);
		bg.setBounds(0, 0, 800, 600);
		e1.setBounds(76, 50, 64, 64);
		e2.setBounds(148, 50, 64, 64);
		e3.setBounds(217, 50, 64, 64);
		e4.setBounds(281, 50, 64, 64);
		e5.setBounds(338, 50, 64, 64);
		all.setBounds(76, 160, 90, 40);
		allN.setBounds(120, 210, 180, 40);
		profit.setBounds(76, 300, 90, 40);
		profitN.setBounds(120, 340, 180, 40);
		title.setBounds(480, 115, 180, 40);
		
		//字体设置
		Font font = new Font("华文行楷", Font.BOLD, 20); // 字体
		Font f = new Font("幼圆", Font.BOLD, 30); // 字体
		all.setForeground(Color.black); all.setFont(font);
		allN.setForeground(Color.red); allN.setFont(f);
		profit.setForeground(Color.black); profit.setFont(font);
		profitN.setForeground(Color.blue); profitN.setFont(f);
		title.setForeground(Color.black); title.setFont(font);
		
		this.add(bt1);
		this.add(bt2);bt2.setVisible(false);//隐藏按钮
		this.add(bt3);bt3.setVisible(false);
		this.add(bt4);bt4.setVisible(false);
		this.add(bt5);bt5.setVisible(false);
		this.add(bt6);bt6.setVisible(false);
		this.add(line);
		this.add(e1);this.add(e2);this.add(e3);
		this.add(e4);this.add(e5);
		this.add(all);this.add(allN);
		this.add(profit);this.add(profitN);
		this.add(title);
		this.add(update);
		
		e2.setVisible(false);//隐藏按钮
		e3.setVisible(false);e4.setVisible(false);e5.setVisible(false);
		//按钮设置
		bt1.setOpaque(false);//透明
		bt2.setOpaque(false);
		bt3.setOpaque(false);
		bt4.setOpaque(false);
		bt5.setOpaque(false);
		bt6.setOpaque(false);
		bt1.setBorderPainted(false); // 按钮无边框
		bt2.setBorderPainted(false);
		bt3.setBorderPainted(false);
		bt4.setBorderPainted(false);
		bt5.setBorderPainted(false);
		bt6.setBorderPainted(false);
		this.add(circleProgressBar1);
		this.add(bg);
		//按钮监听
		update.addActionListener(new Update());
	}
	class Update implements ActionListener
	{
		public void actionPerformed(ActionEvent e) {
			update.setEnabled(false);
			new Thread() {
				int index = 0;
				int r = 255;
				int g = 0;
				int b = 0;
				@Override
				public void run() {
					while (index <= 70) {
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						if (g != 255) {
							g += 5;
						} else {
							r -= 5;
						}
						Color color = new Color(r, g, b);
						circleProgressBar1.setForegroundColor(color);
						circleProgressBar1.setDigitalColor(color);
						circleProgressBar1.setProgress(index++);
					}
					update.setEnabled(true);
				};

			}.start();
		}
	}
}
